# coding=utf-8
"""
Author: Mr.夏末
@Function: 正则
"""
import re
#正则表达式 https://www.yuque.com/docs/share/3171ce66-873e-4645-ad35-3cf53c21c529
myhtml = """
<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#ffffff"><meta name="description" content="全球领先的中文搜索引擎、致力于让网民更便捷地获取信息，找到所求。百度超过千亿的中文网页数据库，可以瞬间找到相关的搜索结果。"><link rel="shortcut icon" href="
	/favicon.ico
	" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml
	" title="百度搜索" /><link rel="icon" sizes="any" maskhref="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg
	"/><link rel="dns-prefetch" href="//dss0.bdstatic.com
	"/><link rel="dns-prefetch" href="//dss1.bdstatic.com
	"/><link rel="dns-prefetch" href="//ss1.bdstatic.com
	"/><link rel="dns-prefetch" href="//sp0.baidu.com
	"/><link rel="dns-prefetch" href="//sp1.baidu.com
	"/><link rel="dns-prefetch" href="//sp2.baidu.com
	"/><title>百度一下，你就知道</title><style index="newi" type="text/css">
	<body>
	<div class="c-tips-container" id="c-tips-container"></div>
	<script>
	var s_session={
		"logId":"3614294667",
		"seqId":"3614294907",
		"sessionId":"",
		"debug":false,
		"userTips":"{}",
		"curmod":"2",
		"firstmod":"",
		"logoCode":false,
		"isFesLogo":false,
		"sid":"36454_34813_36422_36166_36488_36055_36419_26350_36299_36468_36315_36447",
		"mSid":"",
		"sample_value":"",
		"isLogin":false,
		"agingVoice": "",
		"卡卡": "632232258",
		"Will": "1052949192",
		"Phone": "18874922908"
	};
	window.__async_strategy = 2;
	</script>
	</body>
	</html>
"""
# 取两个字符串之间的内容
res = re.findall(r'" title="(.+?)" />',myhtml)
print(res)

# 取JSON字符串
# 不使用re.S时，则只在每一行内进行匹配，如果存在一行没有，就换下一行重新开始，使用re.S参数以后，正则表达式会将这个字符串看做整体，在整体中进行匹配，
res = re.findall(r's_session=(.*);',myhtml,re.S)
print(res)

# 去掉空字符
print(re.sub(r'\s{2,}','',res[0]))

# 提取连续数字(大括号代表前面这个表达式匹配的次数)
res = re.findall(r'[0-9]{5,8}',myhtml)
print(res)

# 提取QQ号
res = re.findall(r'[1-9][0-9]{7,11}',myhtml)
print(res)

# 提取手机号
res = re.findall(r'1[3-9][0-9]{9}',myhtml)
print(res)

# 提取link标签之间内容（贪心，匹配到最后一个结束符）
res = re.findall(r'<link.*/>',myhtml,re.S)
print(res)

# 提取link标签之间内容（非贪心，匹配到第一个结束就结束）
res = re.findall(r'<link.*?/>',myhtml,re.S)
print(res)

s = "896551965012m89789931359654>"
res = re.findall(r'9\d*?9',s)
print(res)